package admin

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	adminReq "gitee.com/g-qs/gin-vue-admin/server/model/admin/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/response"
	"gitee.com/g-qs/gin-vue-admin/server/model/user"
	"gitee.com/g-qs/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type AdminUserApi struct {
}

// GetUserInfoByID 获取单条用户记录
// @Router /adminUser/getUserInfoByID [get]
func (a *AdminUserApi) GetUserInfoByID(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	if info, err := UserService.FindUserByID(uint(uid)); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(info, "获取成功", c)
	}
	return
}

func (a *AdminUserApi) GetOpenVipRecordByID(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.Atoi(id)
	if vip, err := UserService.GetRecordBy(uint(uid)); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(vip, "获取成功", c)
	}
	return
}

func (a *AdminUserApi) GetAllUserList(c *gin.Context) {
	var au adminReq.UserList
	e := c.ShouldBindQuery(&au)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	if list, total, err := UserService.GetAllUserListByWhere(au); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     au.Page,
			PageSize: au.PageSize,
		}, "获取成功", c)
	}
}

func (a *AdminUserApi) GetScUserList(c *gin.Context) {
	var au adminReq.UserList
	e := c.ShouldBindQuery(&au)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	CentreID := utils.GetUserID(c)
	if list, total, err := UserService.GetScUserListByWhere(au, CentreID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     au.Page,
			PageSize: au.PageSize,
		}, "获取成功", c)
	}
}

func (a *AdminUserApi) UpdateUserInfo(c *gin.Context) {
	var uu user.User
	err := c.ShouldBindJSON(&uu)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err = UserService.UpdateDataById(uu.ID, map[string]interface{}{"steward_id": uu.StewardID, "service_center_id": uu.ServiceCenterID}); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
	return
}

// GetOpenVipRecord 获取开通会员记录
// @Tags Record
// @Summary 获取开通会员记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.User true "获取开通会员记录"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /adminUser/getOpenVipRecord [get]
func (a *AdminUserApi) GetOpenVipRecord(c *gin.Context) {
	var pageInfo adminReq.OpenVipSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if list, total, err := UserService.GetOpenVipRecord(pageInfo, userID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetServiceStationOpenVipRecord 获取服务站开通会员记录【超管】
// @Tags Record
// @Summary 获取服务站开通会员记录【超管】
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.User true "获取服务站开通会员记录【超管】"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /adminUser/getOpenVipRecord [get]
func (a *AdminUserApi) GetServiceStationOpenVipRecord(c *gin.Context) {
	var pageInfo adminReq.OpenVipSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if list, total, err := UserService.GetServiceStationOpenVipRecord(pageInfo, userID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetAllOpenVipRecord 获取全部开通会员记录【超管】
// @Tags Record
// @Summary 获取开通会员记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.User true "获取全部开通会员记录【超管】"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /adminUser/getAllOpenVipRecord [get]
func (a *AdminUserApi) GetAllOpenVipRecord(c *gin.Context) {
	var pageInfo adminReq.OpenVipSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := UserService.GetAllOpenVipRecord(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// OpenVip 开通&续费会员
// @Tags adminUser
// @Summary 开通&续费会员
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /adminUser/openVip [post]
func (a *AdminUserApi) OpenVip(c *gin.Context) {
	id := c.Query("id")
	status := c.Query("types")
	vipType := c.Query("vip_type")
	if err := UserService.UpdateOpenRecord(id, status, vipType); err != nil {
		global.GVA_LOG.Error("操作失败!", zap.Error(err))
		response.FailWithMessage("操作失败", c)
	} else {
		response.Result(200, "", "操作成功", c)
	}
}
